package com.example.study;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author : fang.com
 * @CreatTime : 2021-03-23 20:51
 * @Description : 测试 sleep wait 是否 释放锁
 * @Version :  0.0.1
 */
public class main {

    private static Object OBJ = new Object();
    private static Map<String, String> map = new HashMap<>();

    public static void main(String[] args) throws InterruptedException {
        //业务代码
        Runnable run = () -> {
            synchronized (OBJ) {
                try {
                    System.out.println("current thread is : " + Thread.currentThread().getName());
                    //sleep休眠10s
                    //sleep是不会释放锁的，sleep方法没有，他纯粹是调用系统的park方法死循环等待时间到来
                    // Thread.sleep(500);
                    //wait等待10s
                    //在执行完wait后，会有一个wait池，线程会聚集在wait池里等待
                    OBJ.wait(500);
                    //System.out.println(Thread.currentThread().getName() + " ttttt");
                    map.put(Thread.currentThread().getName(), "ttttt");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        new Thread(run, "t1").start();
        new Thread(run, "t2").start();
        new Thread(run, "t3").start();
        Thread.sleep(2000);
        System.out.println(map);
    }


}
